package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LeaveInformation;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ILeaveInformationService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请假信息Controller
 * 
 * @author ruoyi
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/system/approval")
public class LeaveInformationApprovalController extends BaseController
{
    @Autowired
    private ILeaveInformationService leaveInformationService;
    @Autowired
    private ISysPostService postservice;
    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询请假信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:approval:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveInformation leaveInformation)
    {
    	Long useId = SecurityUtils.getLoginUser().getUser().getUserId();
    	int bs=0;
    	int adminTy=0;
    	leaveInformation.setApprovalResults("0");
    	//查询岗位
    	List<Long> postIds = sysPostService.selectPostListByUserId(useId);
    	if(postIds!=null&&postIds.size()>0) {
    		for(Long id:postIds) {
    			 SysPost post=postservice.selectPostById(id);
    			if(post!=null&&("se".equals(post.getPostCode())||"ceo".equals(post.getPostCode()))) {
    				bs=1;
    				break;
    			}
    			if(post!=null&&"ceo".equals(post.getPostCode())) {
    				adminTy=1;
    				break;
    			}
    			
    		}
    	}
    	if(bs==1) {
    		if(adminTy!=1) {
    			leaveInformation.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
    		}
    	}else {
    		leaveInformation.setCreateBy(useId.toString());
    	}
        startPage();
        List<LeaveInformation> list = leaveInformationService.selectLeaveInformationList(leaveInformation);
        return getDataTable(list);
    }

    /**
     * 导出请假信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:approval:export')")
    @Log(title = "请假信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveInformation leaveInformation)
    {
        List<LeaveInformation> list = leaveInformationService.selectLeaveInformationList(leaveInformation);
        ExcelUtil<LeaveInformation> util = new ExcelUtil<LeaveInformation>(LeaveInformation.class);
        util.exportExcel(response, list, "请假信息数据");
    }

    /**
     * 获取请假信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:approval:query')")
    @GetMapping(value = "/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
    {
        return AjaxResult.success(leaveInformationService.selectLeaveInformationByLineId(lineId));
    }


    /**
     * 修改请假信息
     */
    @PreAuthorize("@ss.hasPermi('system:approval:edit')")
    @Log(title = "请假信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveInformation leaveInformation)
    {
    	String name=SecurityUtils.getLoginUser().getUser().getUserName();
    	 leaveInformation.setUpdateBy(name);
    	 leaveInformation.setUpdateTime(new Date());
        return toAjax(leaveInformationService.updateLeaveInformation(leaveInformation));
    }

}
